package com.tuanzi.loan.web.bean.receipt.user;

import com.tuanzi.loan.business.entity.receipt.user.ReceiptSystemUser;
import com.tuanzi.loan.business.entity.receipt.user.ReceiptSystemUserStatus;
import com.tuanzi.loan.business.service.receipt.user.ReceiptSystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ReceiptSystemUserInfoBean extends JSFInfoBean<ReceiptSystemUser> {

	private static final long serialVersionUID = -2152778554108785342L;
	@Autowired
	private ReceiptSystemUserService receiptSystemUserService;

	@PostConstruct
	private void init() {
		String id = getParamFromRequest("id");
		if (StringUtils.isNotBlank(id)) {
			$ = receiptSystemUserService.findOne(id);
			filterSensitive($);
		} else {
			$ = new ReceiptSystemUser();
			changeStatusToAdd();
		}
	}

	/*public void onSave() {
		$ = receiptSystemUserService.saveOrUpdate($);
		changeStatusToView();
		addSuccessfullyMessage();
	}*/

	public void onApprove(String id) {
		receiptSystemUserService.approve(id);
		$.setStatus(ReceiptSystemUserStatus.NORMAL);
		addSuccessfullyMessage();
	}

	public void onReject(String id) {
		receiptSystemUserService.reject(id);
		$.setStatus(ReceiptSystemUserStatus.FAIL);
		addSuccessfullyMessage();
	}
	
	private void filterSensitive(ReceiptSystemUser user) {
		user.setName(PhoneNumNideUtil.hideName(user.getName()));
		user.setPhone(PhoneNumNideUtil.hideNum(user.getPhone()));
	}

}
